/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClassRoomGui.java
 *
 * Created on 17/06/2011, 11:55:39 AM
 */

package GUI.ATTENDANT;

import Procedures.DBConection;
import entities.ComputerRoom;

/**
 *
 * @author SIX-14
 */
public class ComputerRoomGui extends javax.swing.JPanel {

    /** Creates new form ClassRoomGui */
    public ComputerRoomGui(DBConection myConection, int action, ComputerRoom computerRoom) {
        initComponents();
        setMyConection(myConection);
        setAction(action);
        setComputerRoom(computerRoom);
        initComponents2();

    }

    private DBConection myConection;
    private int action;
    private ComputerRoom computerRoom;

    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public ComputerRoom getComputerRoom() {
        return computerRoom;
    }

    public void setComputerRoom(ComputerRoom computerRoom) {
        this.computerRoom = computerRoom;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public void  initComponents2(){
        switch(getAction()){
            case 1: jButton1.setText("Agregar"); break;
            case 2: jButton1.setText("Modificar"); jCheckBox1.setEnabled(true); fillfields(); break;
            case 3: jButton1.setText("Eliminar"); jTextField1.setEditable(false); jCheckBox1.setEnabled(false); fillfields(); break;
        }
    }

    public void fillfields(){
        jTextField1.setText(getComputerRoom().getLetter());
        jCheckBox1.setSelected(getComputerRoom().isActive());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();

        jLabel1.setText("Letra de la sala:");

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Activa");
        jCheckBox1.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jCheckBox1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jButton1)))
                .addContainerGap(286, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBox1)
                .addGap(13, 13, 13)
                .addComponent(jButton1)
                .addContainerGap(161, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(jButton1.getText().equals("Agregar")){
            getMyConection().sendSQL("INSERT INTO COMPUTER_ROOM SET LETTER='"+jTextField1.getText()+"', active=1;");
        }else if(jButton1.getText().equals("Modificar")){
            int active;
            if(jCheckBox1.isSelected()){
                active=1;
            }else{
                active=0;
            }
            getMyConection().sendSQL("UPDATE COMPUTER_ROOM SET LETTER='"+jTextField1.getText()+"', ACIVE="+active+" WHERE LETTER='"+getComputerRoom().getLetter()+"';");
        }else if(jButton1.getText().equals("Eliminar")){
            getMyConection().sendSQL("UPDATE COMPUTER_ROOM SET ACTIVE=0 WHERE  LETTER='"+jTextField1.getText()+"';");
        }
    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
